let { HttpRequest, globalData, mfutil, UserAccess, addUserStat, URLChangeCommon } = getApp()
const app = getApp();
Page({
  data: {
    active: null,
    radioActive: false,
    json: {
      tile_info: null
    },
    router: '/pages/member-privilege-detail/member-privilege-detail',
    pagetitle: '会员特权详情',
    sucessTile: false,
    scrolly: true,
    scrollTop: 0,
    loadingActive: false
  },
  onLoad(options) {
    console.log(options);
    my.hideShareMenu({
      success() {
      },
      fail() {
      },
    });
    let { active, id, activetype, listbuttontext } = options;
    console.log(activetype);
    /**
  * 用户行为记录埋点
  * 页面初始化
  */
    this.setData({
      active,
      id,
      activetype,
      listbuttontext
    }, async () => {
      await this.initPage(activetype)
      let routerArr = getCurrentPages();
      let UserAccessoptions = {
        grade: routerArr.length > 1 ? routerArr[routerArr.length - 2].data.router : '',
        name: routerArr[routerArr.length - 1].data.pagetitle,
        address: this.data.json.tile_info.id
      }
      let track = UserAccess(UserAccessoptions)
    })

    console.log(this.data);


    // 自定义埋点
    let customAccessoptions = {
      customType: 'TileCover_' + id,
      // tileId: id
    }
    console.log(id, '进入Tile封面');

    let tracks = addUserStat(customAccessoptions)


  },
  async initPage(activetype) {
    console.log(globalData.userInfo.memberId);

    let { id } = this.data;
    let parmasObj = {
      member_id: globalData.userInfo.memberId,
      member_id_type: 1,
    }
    let path = `/vans-family/api/v2.1/project/vansFamily/cms/forward/get?url=/v2/cms/api/tile/${id}`;
    this.setData({
      loadingActive: true
    })
    let res = await HttpRequest(path, 'POST', parmasObj, {});
    console.log(res.data);

    if (res.data.code == 0) {
      this.setData({
        json: res.data.data
      })
    }
    this.setData({
      loadingActive: false
    })
  },
  async componentTile() {
    let { id, activetype, json } = this.data;
    console.log("77777");
    let time = new Date(+new Date() + 8 * 3600 * 1000).toJSON().substr(0, 19).replace("T", " ").replace(/\-/g, "/")
    let bodyObj1 = {
      do_datetime: time,
    }
    let parmasObj = {
      member_id_type: 1,
      member_id: globalData.userInfo.memberId
    }
    console.log("8888888");
    console.log(json.related.done, "9999999999999999999"); //done  以进行次数
    console.log(json.tile_info.limit, "9999999999999999999111"); // limit  可参与次数

    if (json.related.done < json.tile_info.limit || json.tile_info.limit==0 ) {
      console.log("888899999");
      if (activetype != 'finished') {
        console.log("99991000");
        let path1 = `/vans-family/api/v2.1/project/vansFamily/cms/forward/post?url=/v1/cms/api/tile/${json.tile_info.type}/${id}/done`;
        let result = await HttpRequest(path1, 'POST', parmasObj, bodyObj1);
        console.log("10001111111111");
        console.log(result.data, "111111111111111", this.data);
        if (result.data.code == 0) {

          //  if (result.data.code == 0) {
          this.setData({
            sucessTile: true
          })
          console.log("9999999");
        } else if (result.data.code == 53014 || result.data.code == 53001 || result.data.code == 0) {
          console.log("10_____++");
          result.data.code != 0 && my.alert({
            content: result.data.msg
          })
          console.log("12_____++");
        } else {
          console.log("11___________++");
          mfutil.launchErrPage(53001)
        }
      }
    }

    /**
  * 用户行为记录埋点
  */
    let routerArr = getCurrentPages();
    let options = {
      grade: routerArr.length > 1 ? routerArr[routerArr.length - 2].data.router : '',
      name: routerArr[routerArr.length - 1].data.pagetitle,
      address: this.data.json.tile_info.id,
    }
    let track = UserAccess(options)

    // 自定义埋点
    let customAccessoptions = {
      customType: 'TileDone_' + id + '_活动完成TileDone',
      // tileId: id,
      // points: this.data.json.tile_info.points
    }
    console.log(id, 'TileDone');

    let tracks = addUserStat(customAccessoptions)
  },
  memberPrivilege() {
    // if (json.tile_info.required_tc && radioActive) {
    //   this.setData({
    //     activetype: 'finished'
    //   })
    // } else if (!json.tile_info.required_tc) {
    //   this.setData({
    //     activetype: 'finished'
    //   })
    // }

    // my.switchTab({
    //   url: '/pages/member-privilege-page/member-privilege-page',
    //   success() {
    //     // let a = getCurrentPages();
    //     // a[a.length - 2].onLoad()
    //   }
    // })
  },
  wangwang() {
    my.tb.openMessage({
      sellerNick: "商家测试账号",
      success: (res) => {
      },
      fail: (res) => {
      },
    })
  },
  //单选协议同意/默认
  radioChange() {
    let { radioActive } = this.data;
    this.setData({
      radioActive: !radioActive
    })
  },
  /**
 * 链接文字点击
 */
  hyperlinkChange() {
    let { hyperlinkAcitve } = this.data;
    this.setData({
      hyperlinkAcitve: !hyperlinkAcitve
    })
  },
  urlChange(event) {
    try {
      let { item } = event.currentTarget.dataset;
      // 判断当前 type 是否为 post  ,tile限制次数，并且用户未完成过，或者不限制次数调用tiledone接口
      console.log("111111");
      let done = this.data.json.related.done;
      let limit = this.data.json.tile_info.limit;
      console.log(done,"donendonedone");
      console.log(limit,"limitlimitlimtii");
      console.log("2222222");

      if (this.data.json.tile_info.type != 'post' && (done < limit && limit >= 1) || limit == 0) {
        console.log("333333");

        this.componentTile();
        console.log("=============================================================", item)
      }
      console.log("444444");

      URLChangeCommon(item)
      console.log("5555555");

      /**
  * 用户行为记录埋点
  */
      let routerArr = getCurrentPages();
      let UserAccessoptions = {
        grade: routerArr.length > 1 ? routerArr[routerArr.length - 2].data.router : '',
        name: routerArr[routerArr.length - 1].data.pagetitle,
        address: this.data.json.tile_info.id,
        String4: item.cta_button_name,
      }
      let track = UserAccess(UserAccessoptions)

      // 自定义埋点
      let customAccessoptions = {
        customType: 'TileCTA_' + this.data.json.tile_info.id + '_' + item.cta_button_name,
        // tileId: this.data.json.tile_info.id,
        // ctabtn: this.data.json.tile_info.cta_button_name
      }

      let tracks = addUserStat(customAccessoptions)
      console.log("666666");
    } catch (err) {
      console.log(err);
    }

  },
  onScroll(event) {
    this.setData({
      scrollTop: event.detail.scrollTop,
      ScrollToUpper: scrollTop == 0 ? true : false
    })
    if (scrollTop == 0) {
      this.setData({
        ScrollToUpper: true
      })
    }
  },
  onTouchStart(event) {
    console.log(event, 'onTouchStartonTouchStart')
    this.setData({
      starty: event.changedTouches[0].pageY,
    })
  },
  onTouchMove(event) {
    //当前pagey
    this.setData({
      movey: event.changedTouches[0].pageY
    })
    let { scrollTop } = this.data;
    let { ScrollToUpper, ScrollToLower, movey, starty } = this.data;
    if (ScrollToUpper && (starty > movey)) {
      this.setData({
        scrolly: true,
        ScrollToUpper: false,
        ScrollToLower: false
      })
    } else
      if (ScrollToUpper && (starty < movey)) {
        this.setData({
          scrolly: false,
        })
      }
    if (ScrollToLower && (starty < movey)) {
      this.setData({
        scrolly: true,
        ScrollToUpper: false,
        ScrollToLower: false
      })
    } else
      if (ScrollToLower && (starty > movey)) {
        this.setData({
          scrolly: false,
        })
      }
  },
  onScrollToUpper(event) {
    this.setData({
      scrolly: false,
      ScrollToUpper: true,
      ScrollToLower: false
    })
  },
  onScrollToLower() {
    this.setData({
      scrolly: false,
      ScrollToLower: true,
      ScrollToUpper: false
    })
  }
});
